home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World 2006 November
/
PCWorld_2006-11_cd.bin
/
domacnost a kancelar
/
findgraph
/
fgraph.exe
/
{app}
/
TestVC
/
RectItem.cpp
< prev
next >
Wrap
C/C++ Source or Header
|
2002-08-09
|
9KB
|
391 lines
// RectItem.cpp : implementation of the CRectItem class
//
#include "stdafx.h"
#include "Crov.h"
#include "MainDoc.h"
#include "MainView.h"
#include "RectItem.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CRectItem implementation
IMPLEMENT_SERIAL(CRectItem, COleClientItem, 0)
CRectItem::CRectItem(COleDocument* pContainer) : COleClientItem(pContainer) ,
m_ptPos(10, -10), m_sizeContent(0,0), m_sizeIcon(0,0),
m_sizeContentExtent(0, 0), m_sizeIconExtent(0, 0)
{
}
CRectItem::CRectItem(COleDocument* pContainer, int x, int y) : COleClientItem(pContainer) ,
m_sizeContent(0,0), m_sizeIcon(0,0),
m_sizeContentExtent(0, 0), m_sizeIconExtent(0, 0)
{
m_ptPos.x = x;
m_ptPos.y = y;
}
CRectItem::CRectItem() :
m_ptPos(10, -10), m_sizeContent(0,0), m_sizeIcon(0,0),
m_sizeContentExtent(0, 0), m_sizeIconExtent(0, 0)
{
}
CRectItem::~CRectItem()
{
}
void CRectItem::Invalidate(CView* pNotThisView)
{
GetDocument()->UpdateAllViews(pNotThisView, 0, this);
}
/////////////////////////////////////////////////////////////////////////////
// CRectItem diagnostics
#ifdef _DEBUG
void CRectItem::AssertValid() const
{
COleClientItem::AssertValid();
}
void CRectItem::Dump(CDumpContext& dc) const
{
COleClientItem::Dump(dc);
}
#endif
/////////////////////////////////////////////////////////////////////////////
CSize CRectItem::GetSize()
{
if (!this) return CSize(0, 0);
ASSERT_VALID(this);
DVASPECT dv = GetDrawAspect();
if (dv == DVASPECT_ICON)
return m_sizeIcon;
else
return m_sizeContent;
}
CSize CRectItem::GetBaseSize()
{
DVASPECT dv = GetDrawAspect();
if (dv == DVASPECT_ICON)
return m_sizeIconExtent;
else
return m_sizeContentExtent;
}
void CRectItem::SetSize(CSize size)
{
DVASPECT dv = GetDrawAspect();
if (dv == DVASPECT_ICON)
m_sizeIcon = size;
else
m_sizeContent = size;
}
void CRectItem::SetBaseSize(CSize size)
{
ASSERT_VALID(this);
DVASPECT dv = GetDrawAspect();
if (dv == DVASPECT_ICON)
m_sizeIconExtent = size;
else
m_sizeContentExtent = size;
}
void CRectItem::SetRect(CRect& rect)
{
m_ptPos = rect.TopLeft();
SetSize(rect.Size());
}
BOOL CRectItem::UpdateExtent()
{
// get size in pixels
CSize size;
if (!GetCachedExtent(&size))
return FALSE; // blank
Invalidate(); // invalidate the old size/position
CSize sizeBase = GetBaseSize();
if (size == sizeBase) // no change
return FALSE;
// if new object (i.e. m_extent is empty) setup position
if (sizeBase == CSize(0,0))
{
// convert to document coords
CSize sizeNew(MulDiv(size.cx, 10, 254), - MulDiv(size.cy, 10, 254));
SetSize(sizeNew);
}
else
{
if (!IsInPlaceActive() && size != sizeBase)
{
// data changed and not inplace, so scale up rect as well
CSize sizeCur = GetSize();
sizeCur.cx = MulDiv(sizeCur.cx, size.cx, sizeBase.cx);
sizeCur.cy = - MulDiv(-sizeCur.cy, size.cy, sizeBase.cy);
SetSize(sizeCur);
}
}
SetBaseSize(size);
Invalidate(); // as well as the new size/position
return TRUE;
}
BOOL CRectItem::OnChangeItemPosition(const CRect& rectPos)
{
CMainView* pView = GetActiveView();
if (pView == NULL)
return FALSE;
ASSERT_VALID(pView);
CRect rc = rectPos;
pView->ClientToDoc(rc);
if (rc != GetRect())
{
// invalidate old item
Invalidate();
// update to new rectangle
SetRect(rc);
GetDocument()->SetModifiedFlag();
CSize sizeExtent;
GetCachedExtent(&sizeExtent);
SetBaseSize(sizeExtent);
// and invalidate new
Invalidate();
}
return COleClientItem::OnChangeItemPosition(rectPos);
}
void CRectItem::OnActivate()
{
// allow only one inplace active item per frame
CMainView* pView = GetActiveView();
ASSERT_VALID(pView);
COleClientItem* pItem = GetDocument()->GetInPlaceActiveItem(pView);
if (pItem != NULL && pItem != this)
pItem->Close();
COleClientItem::OnActivate();
// set selection to an item when it becomes active
pView->SetSelection(this);
}
void CRectItem::OnDeactivateUI(BOOL bUndoable)
{
COleClientItem::OnDeactivateUI(bUndoable);
// hide the object if it is not an outside-in object
ASSERT_VALID(this);
DWORD dwMisc = 0;
m_lpObject->GetMiscStatus(GetDrawAspect(), &dwMisc);
if (dwMisc & OLEMISC_INSIDEOUT)
DoVerb(OLEIVERB_HIDE, NULL);
}
void CRectItem::OnChange(OLE_NOTIFICATION nCode, DWORD dwParam)
{
COleClientItem::OnChange(nCode, dwParam);
switch(nCode)
{
case OLE_CHANGED:
UpdateExtent();
Invalidate();
break;
case OLE_CHANGED_ASPECT:
case OLE_CHANGED_STATE:
Invalidate();
break;
}
}
void CRectItem::OnGetItemPosition(CRect& rPosition)
{
ASSERT_VALID(this);
if (GetSize() == CSize(0,0))
UpdateExtent();
// copy m_rect, which is in document coordinates
rPosition = GetRect();
CMainView* pView = GetActiveView();
ASSERT_VALID(pView);
pView->DocToClient(rPosition);
}
void CRectItem::Move(CRect &rc)
{
// invalidate old rect
Invalidate();
// invalidate new
SetRect(rc);
Invalidate();
// update item rect when in-place active
if (IsInPlaceActive())
SetItemRects();
}
void CRectItem::ResetSize()
{
ASSERT_VALID(this);
Invalidate();
SetBaseSize(CSize(0, 0));
UpdateExtent();
}
/////////////////////////////////////////////////////////////////////////////
void CRectItem::Serialize(CArchive& ar)
{
ASSERT_VALID(this);
// Call base class first to read in COleClientItem data.
// Since this sets up the m_pDocument pointer returned from
// CRectItem::GetDocument, it is a good idea to call
// the base class Serialize first.
CRect rect;
// IMPORTANT: when using "easy" serialize -- call base class FIRST!
// (not strictly necessary, but a good idea)
COleClientItem::Serialize(ar);
// now store/retrieve data specific to CRectItem
if (ar.IsStoring())
{
WORD w = 0x5500; // magic value
ar << w << m_ptPos;
ar << m_sizeIcon << m_sizeIconExtent;
ar << m_sizeContent << m_sizeContentExtent;
}
else
{
WORD w;
ar >> w >> m_ptPos;
ar >> m_sizeIcon >> m_sizeIconExtent;
ar >> m_sizeContent >> m_sizeContentExtent;
if (w != 0x5500)
{
TRACE0("Bad magic number in front of an item wnd\n");
AfxThrowArchiveException(CArchiveException::generic);
}
}
}
/////////////////////////////////////////////////////////////////////////////
// OnGetClipboardData is used by CopyToClipboard and DoDragDrop
COleDataSource* CRectItem::OnGetClipboardData(
BOOL bIncludeLink, LPPOINT lpOffset, LPSIZE lpSize)
{
ASSERT_VALID(this);
COleDataSource* pDataSource = new COleDataSource;
TRY
{
GetNativeClipboardData(pDataSource);
GetClipboardData(pDataSource, bIncludeLink, lpOffset, lpSize);
}
CATCH_ALL(e)
{
delete pDataSource;
THROW_LAST();
}
END_CATCH_ALL
ASSERT_VALID(pDataSource);
return pDataSource;
}
void CRectItem::GetNativeClipboardData(COleDataSource* pDataSource)
{
ASSERT_VALID(this);
ASSERT_VALID(GetDocument());
// Create a shared file and associate a CArchive with it
CSharedFile file;
CArchive ar(&file, CArchive::store);
// Serialize selected objects to the archive
Serialize(ar);
ar.Close();
pDataSource->CacheGlobalData(CMainDoc::m_cfPrivate, file.Detach());
}
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
// From MSDN: HOWTO: IQ: Q184663
/*******************************************************************
* This method returns the IDispatch* for the application linked to
* this container.
********************************************************************/
LPDISPATCH CRectItem::GetIDispatch()
{
//The this and m_lpObject pointers must be valid for this function
//to work correctly. The m_lpObject is the IUnknown pointer to
// this object.
ASSERT_VALID(this);
ASSERT(m_lpObject != NULL);
LPUNKNOWN lpUnk = m_lpObject;
//The embedded application must be running in order for the rest
//of the function to work.
Run();
//QI for the IOleLink interface of m_lpObject.
LPOLELINK lpOleLink = NULL;
if (m_lpObject->QueryInterface(IID_IOleLink,
(LPVOID FAR*)&lpOleLink) == NOERROR)
{
ASSERT(lpOleLink != NULL);
lpUnk = NULL;
//Retrieve the IUnknown interface to the linked application.
if (lpOleLink->GetBoundSource(&lpUnk) != NOERROR)
{
TRACE0("Warning: Link is not connected!\n");
lpOleLink->Release();
return NULL;
}
ASSERT(lpUnk != NULL);
}
//QI for the IDispatch interface of the linked application.
LPDISPATCH lpDispatch = NULL;
if (lpUnk->QueryInterface(IID_IDispatch, (LPVOID FAR*)&lpDispatch)
!=NOERROR)
{
TRACE0("Warning: does not support IDispatch!\n");
return NULL;
}
//After assuring ourselves it is valid, return the IDispatch
//interface to the caller.
ASSERT(lpDispatch != NULL);
return lpDispatch;
}